/**
 * 
 */
package com.san.simulation.model.impl;

import java.util.List;
import java.util.Map;

import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.impl.pvm.process.ProcessDefinitionImpl;

import com.san.simulation.model.api.Element;
import com.san.simulation.model.api.Path;
import com.san.simulation.model.api.ProcessDefinition;

/**
 * @author Aruna
 * 
 */
public class ActivitiProcessDefinition implements ProcessDefinition {

	private org.activiti.engine.repository.ProcessDefinition definition;
	private List<Path> processPath;
	private Map<String, Element> elementMap = null;
	
	
	public ActivitiProcessDefinition() {
	}
	
	public ActivitiProcessDefinition(org.activiti.engine.repository.ProcessDefinition definition){
		super();
		this.definition=definition;
	}

	public List<Path> getProcessPath() {

		return null;
	}

	public Map<String, Element> getProcessElements() {
		// TODO Auto-generated method stub
		return null;
	}

	public org.activiti.engine.repository.ProcessDefinition getDefinition() {
		return definition;
	}

	public void setDefinition(ProcessDefinitionEntity processDefinitionEntity) {
		// TODO Auto-generated method stub

	}

	public Element getStartElement() {

		return new ActivitiProcessElement(
				((ProcessDefinitionImpl) this.definition).getInitial());
	}

	public String getKey() {

		return this.definition.getKey();
	}

	public Map<String, Element> getElementMap() {

		if (this.elementMap == null) {
			List<ActivityImpl> elements = ((ProcessDefinitionImpl) this.definition)
					.getActivities();
			for (ActivityImpl element : elements) {
				elementMap.put(element.getId(), new ActivitiProcessElement(
						element));
			}

		}

		return elementMap;
	}

	public void setElementProperty(String id, String key, Object value) {
		List<ActivityImpl> elements = ((ProcessDefinitionImpl) this.definition)
				.getActivities();
		for (ActivityImpl element : elements) {
			if (element.getId().equals(id)) {
				element.setProperty(key, value);
			}
		}
	}

	public Object getElementProperty(String id, String key) {

		Object returnObject = null;
		List<ActivityImpl> elements = ((ProcessDefinitionImpl) this.definition)
				.getActivities();
		for (ActivityImpl element : elements) {
			if (element.getId().equals(id)) {
				returnObject = element.getProperty(key);
			}
		}

		return returnObject;
	}
}
